package com.dyz.swordFingerOffer;

import java.util.ArrayList;
import java.util.Collections;

public class PrintMinNumber45 {
    public static void main(String[] args) {
        int [] arr= new int[]{3,32,321};
        String a = new PrintMinNumber45().PrintMinNumber(arr);
        System.out.println(a);
    }
    //递归加回溯
    public String PrintMinNumber(int [] numbers) {
        if(numbers==null||numbers.length==0){
            return new String();
        }
        int size = numbers.length;
        String [] as = new String[size];
        for(int i = 0; i<size;i++){
            as[i] = String.valueOf(numbers[i]);
        }
        ArrayList<String> ans = new ArrayList<>();
        dfs(as, 0, size, ans);

        Collections.sort(ans);
        return  ans.get(0);

    }

    private void dfs(String[] as, int index, int length, ArrayList<String> ans){
        if (index == length-1){
            String s = change(as);
            ans.add(s);
        }else{
            for (int i =index;i<length; i++){
                String tmp = as[i];
                as[i] = as[index];
                as[index] = tmp;
                dfs(as, index+1,length, ans);
                tmp = as[i];
                as[i] = as[index];
                as[index] = tmp;
            }
        }
    }
    private String change(String[] as){
        String rs = "";
        for (String a:as){
            rs +=a;
        }
        return rs;
    }
}
